<?php
/**
 * Created by PhpStorm.
 * User: m1514
 * Date: 2017/1/6
 * Time: 10:00
 */

namespace Common\Model;


class RegModel extends BaseModel
{
    protected $pk='uid';
    protected $tableName='user';
    protected $_validate=[
      array('username','require','请输入用户名'),
      array('password','require','请输入密码'),
      array('email','require','请输入电子邮箱'),
    ];
    //验证码验证方法
    Public function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
    //注册
    public function index(){
        $post=I('post.');
        $code = I('code');
        $id='';
        if(!$this->create()) return false;
        $post=I('post.');
//        dd($post);
        //确认密码验证
        if(!$post['password']=$post['ConfirmPassword']){
            $this->error='确认密码不正确请重新输入';
            return false;
        }
        $post['password']=md5($post['password']);
        //验证码验证
        if(! $this->check_verify($code, $id)){
            $this->error='验证码错误';
            return false;
        }
        $this->add($post);
        return true;
    }
//    登录
    public function Login(){
        $post=I('post.');
        $code = I('code');
        $id='';
        if(!$this->create()) return false;

        $map['username']=$post['username'];
        $pass['password']=$post['password'];
        //密码加密
        $password=md5($post['password']);


        $data=$this->where($map)->find();


        //用户名密码验证
        if(!$data){
            $this->error='用户名错误';
            return false;
        }
        if($data['password']!=$password){
            $this->error='密码不正确';
            return false;
        }

//        dd($data);die;


        //验证码验证
        if(! $this->check_verify($code, $id)){
            $this->error='验证码错误';
            return false;
        }

        $_SESSION['uid']=$data['uid'];
        $_SESSION['username']=$data['username'];
        $_SESSION['account']=$data['account'];
//        dd($_SESSION);die;
        return true;
    }
}